// sis puella oier
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
ll read(){
    ll xx = 0, f = 1; char ch = getchar();
    for (;!isdigit(ch); ch = getchar())f = (ch == '-' ? -1 : 1);
    for (; isdigit(ch); ch = getchar())xx = xx * 10 + ch - '0';
    return xx * f;
}
const int N = 200100, inf = 1e15, M = 1000100;
int V;
struct sqtr{
    int tr[M << 2], lt[M << 2];
    void build(int x, int l, int r){
        tr[x] = -inf, lt[x] = 0;
        if (l == r){
            if (l == 0)tr[x] = 0;
            return ;
        }
        int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
    }
    void doit(int x, int val){tr[x] += val, lt[x] += val;}
    void down(int x){
        if (lt[x] != 0)doit(x << 1, lt[x]), doit(x << 1 | 1, lt[x]), lt[x] = 0;
    }
    void up(int x){tr[x] = max(tr[x << 1], tr[x << 1 | 1]);}
    void add(int x, int l, int r, int nl, int nr, int val){
        if (nl <= l && r <= nr){doit(x, val); return ;}
        int mid = (l + r) >> 1; down(x);
        if (nl <= mid)add(x << 1, l, mid, nl, nr, val);
        if (nr > mid)add(x << 1 | 1, mid + 1, r, nl, nr, val);
        up(x);
    }
    void upd(int x, int l, int r, int nl, int val){
        if (l == r){
            tr[x] = max(tr[x], val);
            return ;
        }
        int mid = (l + r) >> 1; down(x);
        if (nl <= mid)upd(x << 1, l, mid, nl, val);
        else upd(x << 1 | 1, mid + 1, r, nl, val);
        up(x);
    }
    int query(int x, int l, int r, int nl, int nr){
        if (nl <= l && r <= nr)return tr[x];
        int mid = (l + r) >> 1; down(x);
        if (nr <= mid)return query(x << 1, l, mid, nl, nr);
        if (nl > mid)return query(x << 1 | 1, mid + 1, r, nl, nr);
        return max(query(x << 1, l, mid, nl, nr), query(x << 1 | 1, mid + 1, r, nl, nr));
    }
    void build(){build(1, 0, V);}
    void add(int nl, int nr, int val){add(1, 0, V, nl, nr, val);}
    void upd(int nl, int val){upd(1, 0, V, nl, val);}
    int query(int nl, int nr){if (nl > nr)return -inf; return query(1, 0, V, nl, nr);}
}A;
int n, a[N];
int calc(int x, int y){if (x == y)return x; else return 0;}
void solve(){
    n = read(); V = 0; for (int i = 1; i <= n; ++i)a[i] = read(), V = max(V, a[i]);
    A.build(); a[0] = 0;
    for (int i = 1; i <= n; ++i){
        int val = max(max(A.query(0, a[i] - 1), A.query(a[i] + 1, V)), A.query(a[i], a[i]) + a[i]);
        A.add(0, V, calc(a[i - 1], a[i])), A.upd(a[i - 1], val);
    }
    printf("%lld\n", A.query(0, V));
}
signed main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int _ = read();
    while(_--)solve();
    return 0;
}

